//
//  CHSearchView.m
//  CarFile
//
//  Created by 陈浩 on 2017/7/27.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHPickerView.h"
#import "CHSearchView.h"
@interface CHSearchView ()<CHPickerViewDelegate>
@property (nonatomic ,strong) UILabel *dateLabel;
@property (nonatomic ,strong) UITextField *companyField;
@property (nonatomic ,strong) NSString *dateStr;
@end

@implementation CHSearchView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = KRGB(239, 239, 249);
        [self setupUI];
    }
    return self;
}

-(void)setupUI{
    
   
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM"];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *lastMonthComps = [[NSDateComponents alloc] init];
    [lastMonthComps setMonth:-1];
    NSDate *newdate = [calendar dateByAddingComponents:lastMonthComps toDate:currentDate options:0];
    NSString *dateStr = [formatter stringFromDate:newdate];
    self.dateStr = dateStr;
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, UIScreenW, 88)];
    bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:bgView];
    
    UILabel *rqLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
    rqLabel.font = [UIFont systemFontOfSize:14];
    rqLabel.text = @"   日 期";
    [bgView addSubview:rqLabel];
    
   
    UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, UIScreenW - 60 - 35, 44)];
    dateLabel.userInteractionEnabled = YES;
    dateLabel.textAlignment = NSTextAlignmentRight;
    dateLabel.font = [UIFont systemFontOfSize:14];
    dateLabel.text = self.dateStr;
    dateLabel.textColor = [UIColor colorWithHexString:@"999999" alpha:0.7f];
    [bgView addSubview:dateLabel];
    self.dateLabel = dateLabel;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    [dateLabel addGestureRecognizer:tap];
    
    
    UIImageView *arrowImgView = [[UIImageView alloc] initWithFrame:CGRectMake(UIScreenW - 25, 15, 10, 14)];
    arrowImgView.image = [UIImage imageNamed:@"right_arrow"];
    [bgView addSubview:arrowImgView];
    
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(10, 44,UIScreenW - 10 , 1)];
    line.backgroundColor = [UIColor colorWithHexString:@"999999" alpha:0.3f];
    [bgView addSubview:line];
    
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 44, 60, 44)];
    nameLabel.text = @"   名 称";
    nameLabel.font = [UIFont systemFontOfSize:14];
    [bgView addSubview:nameLabel];
    
    
    UITextField *companyField = [[UITextField alloc] initWithFrame:CGRectMake(60, 44, UIScreenW - 60 - 35, 44)];
    companyField.font = [UIFont systemFontOfSize:14];
    companyField.placeholder = @"请输入企业名称";
    companyField.textAlignment = NSTextAlignmentRight;
    [bgView addSubview:companyField];
    self.companyField = companyField;
    
    UIButton *resetBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, self.h - 40, UIScreenW /2, 40)];
    [resetBtn setTitle:@"重置" forState:UIControlStateNormal];
    resetBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [resetBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    resetBtn.backgroundColor = [UIColor whiteColor];
    [resetBtn addTarget:self action:@selector(resetBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:resetBtn];
    
    
    UIButton *searchBtn = [[UIButton alloc] initWithFrame:CGRectMake(UIScreenW /2, self.h - 40, UIScreenW /2, 40)];
    searchBtn.backgroundColor =  [UIColor colorWithHexString:@"1692c8" alpha:1.0f];
    searchBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [searchBtn setTitle:@"查找" forState:UIControlStateNormal];
    [searchBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [searchBtn addTarget:self action:@selector(searchBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:searchBtn];
    
}

-(void)tapGesture:(UITapGestureRecognizer *)tap{
    
    [self.companyField resignFirstResponder];
    CHPickerView *pickerView = [CHPickerView pickerView];
    pickerView.delegate = self;
    [pickerView show];
  
}


#pragma mark - CHPickerViewDelegate
-(void)pickerViewDidBackToDateStr:(NSString *)dateStr{
    
    self.dateLabel.text = dateStr;
    self.dateLabel.textColor = [UIColor blackColor];
}




-(void)resetBtnClick{
   
    self.dateLabel.text = self.dateStr;
    self.companyField.text = nil;
    self.dateLabel.textColor = [UIColor colorWithHexString:@"999999" alpha:0.7f];
    
}

-(void)searchBtnClick{
    
    if ([self.delegate respondsToSelector:@selector(didSearchToDateStr: companyName:)]) {
        [self.delegate didSearchToDateStr:self.dateLabel.text companyName:self.companyField.text];
    }
    
    
}

@end
